package com.l.web.service;

import com.l.web.service.system.config.Config;
import com.l.web.service.system.config.ConfigDAO;
import com.l.web.service.system.user.User;
import com.opensymphony.xwork2.ActionContext;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.l.qiao.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public abstract class ServiceSupport {

    protected static final String TYPE = "_type";
    protected static final String TYPE_SAVE = "save";

    public static final String SUCCESS = "success";
    public static final String NONE = "none";
    public static final String LIST = "list";
    public static final String ADD = "add";
    public static final String DETAIL = "detail";
    public static final String SAVE = "save";
    public static final String UPDATE = "update";
    public static final String DELETE = "delete";

    protected ActionContext context;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected PrintWriter writer = null;
    protected Form form;

    private static final Logger LOGGER = Logger.getLogger(ServiceSupport.class);
    private ConfigDAO configDAO;

    public ServiceSupport() {
        context = ActionContext.getContext();
        request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
        response = (HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE);
        form = new Form(request);
    }

    public void setConfigDAO(ConfigDAO configDAO) {
        this.configDAO = configDAO;
    }

    public Config getConfigure(String key) {
        Cluster cluster = configDAO.getConfig(key);
        Config config = new Config((Map) Global.config.clone());
        ClusterUtil.extend((Map) config.clone(), cluster);
        return config;
    }

    public User getLoginUser() {
        return (User) context.getSession().get(Global.SESSION_USER);
    }

    public Page getPage() {
        Page page = new Page();
        form.getBean(page, false, "page");
        return page;
    }

    public KeyUpdate getKeyUpdate(Class clazz) {
        KeyUpdate keyUpdate = new KeyUpdate(clazz);
        form.getBean(keyUpdate, false, "update");
        return keyUpdate;
    }

    public abstract String execute();

    public String error(int code) {
        try {
            response.sendError(code);
        } catch (IOException e) {
            LOGGER.error("设置http状态错误(" + code + "):" + e);
        }
        return NONE;
    }

    public boolean out(int code) {
        ResponseInfo responseInfo = new ResponseInfo(code);
        return out(responseInfo);
    }

    public boolean out(Object content) {

        if (writer == null) {
            try {
                writer = response.getWriter();
            } catch (IOException e) {
                LOGGER.error("获取页面输出句柄异常：" + e);
                return false;
            }
        }

        writer.write(String.valueOf(content));
        return true;
    }
}
